<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'医馆发卡排行'),
);
$plugin_name=$this->pluginname;
//客户列表
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:100;
	$allparams=$orderparams=$lookparams=$look_allparams=$look_monthparams=array('uniacid'=>$_SESSION['uniacid'],'pluginname'=>$plugin_name);
	if($_RQ['looktime']){
		$time_range=cfc('tools')->base64_decode($_RQ['looktime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$between=array($time_range['tstart'],$time_range['tend']);
		$return['params']['looktime']=$time_range;
	}
	$list=pdo_getslice('jy_yiliao_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$mounthtime =  mktime(0,0,0,date('m'),1,date('Y'));
		$look_allparams['looktime >']=0;
		$lookparams['looktime >']=$daytime;
		$look_monthparams['looktime >']=$mounthtime;
		$orderparams['createtime >']=$daytime;
		foreach($list as &$item){
			$allparams['storeid']=$orderparams['storeid']=$lookparams['storeid']=$look_allparams['storeid']=$look_monthparams['storeid']=$item['id'];
			$item['look_day']=pdo_count('jy_kabao_batch_code',$lookparams);
			$item['code_day']=pdo_count('jy_kabao_batch_code',$orderparams);
			$item['order_day']=pdo_count('jy_kabao_batch_code',$orderparams);
			$item['look_all']=pdo_count('jy_kabao_batch_code',$look_allparams);
			$item['code_all']=pdo_count('jy_kabao_batch_code',$allparams);
			$item['order_all']=pdo_count('jy_kabao_order',$allparams);
			$item['look_month']=pdo_count('jy_kabao_batch_code',$look_monthparams);
		}
	}
	$heji=array('title'=>'合计');
	paixu($list,'baofee','desc');
	foreach($list as $k=>&$v){
		$v['rank']=$k+1;
		$heji['look_day'] +=$v['look_day'];
		$heji['code_day'] +=$v['code_day'];
		$heji['order_day'] +=$v['order_day'];
		$heji['look_all'] +=$v['look_all'];
		$heji['code_all'] +=$v['code_all'];
		$heji['order_all'] +=$v['order_all'];
	}
	$list[]=$heji;
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'rank','title'=>'排名','type'=>'string'),
			array('name'=>'title','title'=>'医馆','type'=>'string'),
			array('name'=>'look_day','title'=>'今日发放','type'=>'string'),
			array('name'=>'look_all','title'=>'总发放','type'=>'string'),
			array('name'=>'code_day','title'=>'今日产卡','type'=>'string'),
			array('name'=>'code_all','title'=>'总产卡','type'=>'string'),
			array('name'=>'order_day','title'=>'今日兑奖','type'=>'string'),
			array('name'=>'order_all','title'=>'总兑奖','type'=>'string'),
			array('name'=>'look_month','title'=>'本月发放','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'医馆名称','name'=>'title','help'=>''),
			//array('name'=>'createtime','width'=>15,'title'=>'时间段','type'=>'time_range')
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:100;
	$allparams=$orderparams=$lookparams=$look_allparams=$look_monthparams=array('uniacid'=>$_SESSION['uniacid'],'pluginname'=>$plugin_name);
	if($_RQ['looktime']){
		$time_range=cfc('tools')->base64_decode($_RQ['looktime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$between=array($time_range['tstart'],$time_range['tend']);
		$return['params']['looktime']=$time_range;
	}
	$list=pdo_getall('jy_yiliao_store',$params,$fields,'','id desc');
	if($list){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$mounthtime =  mktime(0,0,0,date('m'),1,date('Y'));
		$look_allparams['looktime >']=0;
		$lookparams['looktime >']=$daytime;
		$look_monthparams['looktime >']=$mounthtime;
		$orderparams['createtime >']=$daytime;
		foreach($list as &$item){
			$allparams['storeid']=$orderparams['storeid']=$lookparams['storeid']=$look_allparams['storeid']=$look_monthparams['storeid']=$item['id'];
			$item['look_day']=pdo_count('jy_kabao_batch_code',$lookparams);
			$item['code_day']=pdo_count('jy_kabao_batch_code',$orderparams);
			$item['order_day']=pdo_count('jy_kabao_batch_code',$orderparams);
			$item['look_all']=pdo_count('jy_kabao_batch_code',$look_allparams);
			$item['code_all']=pdo_count('jy_kabao_batch_code',$allparams);
			$item['order_all']=pdo_count('jy_kabao_order',$allparams);
			$item['look_month']=pdo_count('jy_kabao_batch_code',$look_monthparams);
		}
	}
	$heji=array('title'=>'合计');
	paixu($list,'baofee','desc');
	foreach($list as $k=>&$v){
		$v['rank']=$k+1;
		$heji['look_day'] +=$v['look_day'];
		$heji['code_day'] +=$v['code_day'];
		$heji['order_day'] +=$v['order_day'];
		$heji['look_all'] +=$v['look_all'];
		$heji['code_all'] +=$v['code_all'];
		$heji['order_all'] +=$v['order_all'];
	}
	$list[]=$heji;
	$header=array(
		'rank'=>'排名',
		'title'=>'医馆',
		'look_day'=>'今日发放',
		'look_all'=>'总发放',
		'code_day'=>'今日产卡',
		'code_all'=>'总产卡',
		'order_day'=>'今日兑奖',
		'order_all'=>'总兑奖',
		'look_month'=>'本月发放',
	);
	model('core/tools')->export2excel($header,$list,'医馆发卡排行');
}